# -*- coding: utf-8 -*-
'''
Created on 2018年6月21日
@author: liaolzl
'''
#1（1）.如果做爬虫爬取网站数据的时候如果不添加headers的话可能会把你禁掉或者出现服务器错误
#例如下面这个例子中，就返回HTTP状态码400，意为非法请求：
import requests
response = requests.get("https://www.zhihu.com/explore")
print(response.text)
#1（2）.如果加入headers，则会返回网页的内容
import requests
headers = {
    'User-Agent':'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36'
}
response = requests.get("https://www.zhihu.com/explore",headers=headers)
print(response.text)

#2(1).基本POST请求1
import requests
data ={'name':'germey','age':'22'}
response = requests.post('http://httpbin.org/post',data=data)
print(response.text)
#2(2).基本POST请求2
import requests
data = {'name':'germey','age':'22'}
headers ={
    'User-Agent':'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36'
}
response = requests.post('http://httpbin.org/post',data=data,headers=headers)
print(response.json())